﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 生成数据库
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Log(string text)
        {
            this.Invoke(new Action(() =>
            {
               
                richTextBox1.AppendText(string.Format("[{0}]{1}", DateTime.Now.ToString("HH:mm:ss:ffff"), text + "\n"));

                richTextBox1.ScrollToCaret();
            }));

        }
        //连接数据库
        private static SqlSugarClient  SqlSugarInstance(string connection)
        {

            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString =@""+connection+";", //必填
                DbType = SqlSugar.DbType.SqlServer, //必填
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.SystemTable
            });
            return db;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult dr = folderBrowserDialog1.ShowDialog();
            if (dr != System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("请选择路径");
            }
            string Connection = textBox1.Text;
           SqlSugarClient db=SqlSugarInstance(Connection);         
            string Name = textBox2.Text;
            if (string.IsNullOrWhiteSpace(Name))
            {
                db.DbFirst.CreateClassFile(""+folderBrowserDialog1.SelectedPath+"");
                Log("文件已经生成到"+folderBrowserDialog1.SelectedPath+"文件里面");
            }
            else
            {
                db.DbFirst.Where(""+ Name + "").CreateClassFile("" + folderBrowserDialog1.SelectedPath + "");
                Log("文件已经生成到"+ folderBrowserDialog1.SelectedPath + "文件里面");
            }
        }
    }
}
